'''
Created on Aug 13, 2012

@author: petrina
'''

import logging
import os
from ConfigParser import ConfigParser



class Config(object):
    '''
    This class sets the user configuration by parsing a config-file (txt).    
    '''
    
    
    def __init__(self, file = None):
        '''
        constructor in order to initialize fields with default values
        
        @param file: filepath of the config-file (absolut or releativ)
        '''

        self._file = None
        if file is not None:
            if os.path.exists(file):
                self._file = file
            else:
                logging.error("Config: invalid path for the config-file (using default parameters)")
        
        self._default = {
                "link_indicators": "stddev_observed,stddev_estimated,stddev_absdiff,buffer_idx,plan_time,plan_speed,buffer_time,buffer_speed,buffer_speed_idx,var_speed,plan_time_idx,plan_speed_idx,completeness,mse,me,corr_coef,total_me,total_mse,total_corr_coef".split(","),
                "network_indicators" : ["nw_completeness"],
                "min_per_int" : '15',
                "is_kph": 'true',
                "links": "*",
                "host":'localhost',
                'name':'fleet',
                'user':'postgres',
                'pass':'postgres',
                'table_link' : 'quattro.road',
                'table_ffspeed' : 'freespeed',
                'table_timeseries' : 'fleet_timeseries',
                'table_data' : 'quattro.fleet_traffic_state',
                'table_reference_data': 'quattrotest.fleet_traffic_state',
                'timeseries_date':'2011-10-01'}


        


    def parse_config(self):
        '''
        parses the file (RFC822 conform)
        @see: http://docs.python.org/library/configparser.html 
        '''

        if self._file is not None:
            self._conf = ConfigParser(self._default)
            self._conf.read(self._file)
        else:
            self._conf = None
            
        return self._conf
